package com.kajespero.dao.velocity.impl;

import java.io.StringWriter;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;

import com.kajespero.dao.velocity.VelocityProvider;

public class VelocityProviderImpl implements VelocityProvider{
	
	private static final Log LOG = LogFactory.getLog(VelocityProviderImpl.class);
	
	private VelocityEngine initVelocity(){
		VelocityEngine engine = new VelocityEngine();
		engine.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute" );
		engine.setProperty("runtime.log.logsystem.log4j.logger", LOG.getClass().getName());
		return engine;
	}

	@Override
	public String templateToHtml(String template, Map<String, Object> map) {
		
		VelocityEngine engine = initVelocity();
		
		VelocityContext context = new VelocityContext(map);

        StringWriter stringWriter = new StringWriter();
        
        engine.evaluate(context, stringWriter, "log-velocity", template);

        return stringWriter.toString();
	}
	
	
}
